package com.lry.sso.service.annotation;

import com.lry.sso.service.exception.RetryException;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author 李瑞益
 * @date 2020-01-17 16:50
 */
@Aspect
public class RetryAop {

    private static final Logger logger = LoggerFactory.getLogger(RetryAop.class);

    @Pointcut("@annotation(retry)")
    public void pointCut(Retry retry){

    }

    @Around(value = "pointCut(retry)", argNames = "joinPoint,retry")
    public Object retry(ProceedingJoinPoint joinPoint,Retry retry) throws Throwable {
        int count = 0;
        Object object = null;
        do{
            try {
                object = joinPoint.proceed();
                return object;
            } catch (RetryException e) {
                logger.error("重试错误：{}",e.getMessage());
            }
        }while(count > retry.value());
        return null;
    }

}
